package ch4;

/**
 * @description : 线程安全的验证
 */

public class StringBufferDemo {
    public static void main(String[] args) throws InterruptedException {
        StringBuilder strBuilder = new StringBuilder();
        StringBuffer strBuffer = new StringBuffer();

        for (int i = 0; i < 1000; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 1000; j++) {
                        strBuilder.append("a");
                        strBuffer.append("a");
                    }
                }
            }).start();
        }
        Thread.sleep(1000);
        System.out.println("StringBuilder长度：" + strBuilder.length() + " - StringBuffer长度：" + strBuffer.length());
    }
}

class MyNum {
    private Integer num = 0;

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public synchronized void append(Integer num) {
        this.num = this.num + num;
    }
}
